using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour
{
	public Texture texMoving;
	public Texture texStill;
	public Texture texWin;
	public Texture texSpotted;
	public float MoveSpeed;
    public TrapManager _trapMgr;
    public Transform citrouille;
    public Texture texCitrouilleStand;
    public Texture texCitrouilleWalk;

    private bool _alive;
    private LevelManager _levelMgr;
    

	// Use this for initialization
	void Start ()
	{
        _alive = true;
        _levelMgr = FindObjectOfType(typeof(LevelManager)) as LevelManager;
        _trapMgr = FindObjectOfType(typeof(TrapManager)) as TrapManager;
	}

	private Vector3 lastMousePos = new Vector3();
	private bool lookWithPad = false;
	private bool wasMoving = true; //to start well

	void Update ()
	{
        if (!_alive)
            return;
		//choose controller
		if (lastMousePos != Input.mousePosition)
		{
			lastMousePos = Input.mousePosition;
			lookWithPad = false;
		}
		var lookX = Input.GetAxis("RightStickHorizontal");
		var lookY = Input.GetAxis("RightStickVertical");
		if (lookX != 0 || lookY != 0)
		{
			lookWithPad = true;
		}

		if (lookWithPad)
		{
			var stickDir = new Vector3(lookX, 0, -lookY);
			this.transform.LookAt(this.transform.position + stickDir);
		}
		else
		{
			var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
			this.transform.LookAt(new Vector3(mousePos.x, this.transform.position.y, mousePos.z));
		}

		//left and right are exclusive
		var force = new Vector3();
		if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
			force.x = 1;
		else if (Input.GetKey(KeyCode.Q) || Input.GetKey(KeyCode.LeftArrow))
			force.x = -1;

		//up and down are exclusive
		if (Input.GetKey(KeyCode.Z) || Input.GetKey(KeyCode.UpArrow))
			force.z = 1;
		else if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
			force.z = -1;

		force.x += Input.GetAxis("DPadHorizontal");
		force.z += Input.GetAxis("DPadVertical");

		var isMoving = force.magnitude > 0.01f;
		if (isMoving && !wasMoving)
		{
			var anim = this.GetComponent<Anim>();
			anim.speed = 0.05f;
			anim.resetTimer(texMoving, 12);

            if (_trapMgr.ActiveTrap == Const.TRAP_TYPE.COSTUME)
            {
                citrouille.GetComponent<Anim>().resetTimer(texCitrouilleWalk, 2);
            }
		}
		else if (!isMoving && wasMoving)
		{
			var anim = this.GetComponent<Anim>();
			anim.resetTimer(texStill, 1);

            if (_trapMgr.ActiveTrap == Const.TRAP_TYPE.COSTUME)
            {
                citrouille.GetComponent<Anim>().resetTimer(texCitrouilleStand, 1);
            }
		}
		wasMoving = isMoving;

		force.Normalize();
		float timeRatio = Time.deltaTime/Time.fixedDeltaTime;
		this.gameObject.rigidbody.AddForce(force*MoveSpeed*timeRatio);
	}

    void OnCollisionEnter(Collision col)
    {
        GameObject obj = col.gameObject;

        //Cone End
        if (col.gameObject.layer == Const.LAYER_CONE_END)
        {
            //TODO Raycast
            if (obj.transform.parent.GetComponent<Enemy>().TestRaycast(this.gameObject))
            {
                obj.transform.parent.GetComponent<Enemy>().ReparePlayer();
                Dead();
            }               
            
            
        }
        //Cone Start
        else if (col.gameObject.layer == Const.LAYER_CONE_START)
        {
            
            //TODO Raycast
            if (obj.transform.parent.GetComponent<Enemy>().TestRaycast(obj.transform.parent.gameObject))
            {
                if (_trapMgr.ActiveTrap == Const.TRAP_TYPE.COSTUME)
                {
                    obj.transform.parent.GetComponent<Enemy>().ScaryCitrouille();

                    _trapMgr.UseTrap();

                    return;
                }

                obj.transform.parent.GetComponent<Enemy>().ReparePlayer();
                Dead();
            }
            
        }


    }

    

    void Dead()
    {
        _alive = false;

        StartCoroutine(WaitForDead());
    }

    IEnumerator WaitForDead()
    {
        yield return new WaitForSeconds(Const.TIME_DEAD);

        _levelMgr.EndStage(false);
    }

}
